package flc.ast.adapter;

import android.graphics.Color;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Random;

import flc.ast.R;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.bean.StkResBean;

public class RankAdapter extends StkProviderMultiAdapter<StkResBean> {

    public RankAdapter() {
        addItemProvider(new StkSingleSpanProvider(126));
        addItemProvider(new RankProvider());
    }

    private class RankProvider extends BaseItemProvider<StkResBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_rank;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, StkResBean bean) {
            if (baseViewHolder.getBindingAdapterPosition() == 0) {
                baseViewHolder.setTextColor(R.id.tvRankItemNum, Color.parseColor("#ED3266"));
            } else if (baseViewHolder.getBindingAdapterPosition() == 1) {
                baseViewHolder.setTextColor(R.id.tvRankItemNum, Color.parseColor("#F18314"));
            } else if (baseViewHolder.getBindingAdapterPosition() == 2) {
                baseViewHolder.setTextColor(R.id.tvRankItemNum, Color.parseColor("#FFD126"));
            } else {
                baseViewHolder.setTextColor(R.id.tvRankItemNum, Color.parseColor("#585A5E"));
            }
            baseViewHolder.setText(R.id.tvRankItemNum, (baseViewHolder.getBindingAdapterPosition() + 1) + "");

            Glide.with(getContext()).load(bean.getThumbUrl())
                    .into((ImageView) baseViewHolder.getView(R.id.ivRankItemImg));
            baseViewHolder.setText(R.id.tvRankItemName, bean.getName());

            String str = new Gson().toJson(bean.getExtra());
            String director = getContext().getString(R.string.director);
            try {
                JSONObject extra = new JSONObject(str);
                JSONArray actorList = extra.getJSONArray("actorList");
                for (int i = 0; i < actorList.length(); i++) {
                    JSONObject item = actorList.getJSONObject(i);
                    if (item.getString("role").equals("导演")) {
                        director += item.getString("name") + " ";
                    }
                }
                baseViewHolder.setText(R.id.tvRankItemDirector, director);
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }

            //评分
            double score = (double) bean.getScore_total() / (bean.getScore_count() == 0 ? 1 : bean.getScore_count());
            String formattedNumber = String.format("%.1f", score);
            baseViewHolder.setText(R.id.tvRankItemScore, formattedNumber);

            Random random = new Random();
            if (baseViewHolder.getBindingAdapterPosition() <= 50) {
                baseViewHolder.setText(R.id.tvRankItemHot, (100 * (50 - baseViewHolder.getBindingAdapterPosition()) + random.nextInt(100)) + "");
            } else {
                baseViewHolder.setText(R.id.tvRankItemHot, 0 + "");
            }
        }
    }

}
